package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.common.R;
import com.example.domain.Employee;
import com.example.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.websocket.server.PathParam;
import java.time.LocalDateTime;
import java.util.List;


@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 员工登录
     *
     * @param request
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
        R login = employeeService.login(employee);
        Employee data = (Employee) login.getData();
        request.getSession().setAttribute("employee", data.getId());
        return login;
    }

    /**
     * 员工退出
     *
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request) {
        //清除员工登录的session
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }


    /**
     * 分页展示，包括查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<IPage<Employee>> page(@PathParam("page")  Integer page, @PathParam("pageSize") Integer pageSize, @PathParam("name") String name) {
        if (name==null){
            name="";
        }
        IPage<Employee> employeeListForPage = employeeService.getEmployeeListForPage(page, pageSize, name);
        R<IPage<Employee>> success = R.success(employeeListForPage);
        return success;
    }

    /**
     * 添加员工
     *
     * @param employee
     * @param request
     * @return
     */
    @PostMapping
    public R<String> addEmployee(@RequestBody Employee employee, HttpServletRequest request) {
        //统一设置初始密码123456并进行MD5加密
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

        //设置创建时间
        //employee.setCreateTime(LocalDateTime.now());
        //设置更新时间
        //employee.setUpdateTime(LocalDateTime.now());

        //设置创建人与更新人，即登录用户的id
        //Employee creater = (Employee) request.getSession().getAttribute("employee");
        //employee.setCreateUser(creater.getId());
        //employee.setUpdateUser(creater.getId());

        if (employeeService.addEmployee(employee)) {
            return R.success("新增员工成功");
        }
        return R.error("添加失败,请重新添加");
    }

    /**
     * 更新员工
     * @param employee 需要更新的员工
     * @param request
     * @return
     */
    @PutMapping
    public R<String> updateEmployee(@RequestBody Employee employee,HttpServletRequest request){
        try {
            //Employee user = (Employee) request.getSession().getAttribute("employee");

            //设置修改时间与人员
            //employee.setUpdateTime(LocalDateTime.now());
            //employee.setUpdateUser(user.getId());

            boolean b = employeeService.updateEmployee(employee);
            return  R.success("修改成功");
        } catch (Exception exception) {
            exception.printStackTrace();
            return R.error("修改失败");
        }
    }

    /**
     * 编辑员工回显数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> queryEmployeeById(@PathVariable Long id){
        try {
            Employee employee = employeeService.getById(id);
            return R.success(employee);
        } catch (Exception exception) {
            exception.printStackTrace();
            return R.error("服务器繁忙");
        }
    }
}
